package com.renli.service.impl;

import com.renli.bean.ResultJson;
import com.renli.mapper.DepartmentMapper;
import com.renli.mapper.RecruitMapper;
import com.renli.pojo.*;
import com.renli.service.RecruitService;
import com.renli.vo.InterviewVo;
import com.renli.vo.SelectPostRelease;
import com.renli.vo.SelectResumeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RecruitServiceImpl implements RecruitService {
    @Autowired
    RecruitMapper recruitMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public ResultJson insertPostRelease(PostRelease postRelease) {
        ResultJson resultJson = new ResultJson();
        if (recruitMapper.insertPostRelease(postRelease) > 0) {
            return resultJson.setSuccess("添加成功");
        } else {
            return resultJson.setFail("添加失败");
        }
    }

    @Override
    public ResultJson selectAllDepartment() {
        ResultJson resultJson = new ResultJson();
        List<Department> list = departmentMapper.selectAllDepartment();
        resultJson.setSuccess("遍历成功", list);
        return resultJson;
    }

    @Override
    public ResultJson selectPost(Post post) {
        ResultJson resultJson = new ResultJson();
        List<Map<String, Object>> list = departmentMapper.selectPost(post);
        resultJson.setSuccess("成功", list);
        return resultJson;
    }

    @Override
    public ResultJson selectPostRelease(SelectPostRelease selectPostRelease) {
        ResultJson resultJson = new ResultJson();
        List<Map<String, Object>> list = recruitMapper.selectPostRelease(selectPostRelease);
        resultJson.setSuccess("成功", list);
        return resultJson;
    }

    @Override
    public ResultJson selectPostClass() {
        ResultJson resultJson = new ResultJson();
        List<PostClass> list = recruitMapper.selectPostClass();
        resultJson.setSuccess("成功", list);
        return resultJson;
    }

    @Override
    public ResultJson updatePostRelease(PostRelease postRelease) {
        ResultJson resultJson = new ResultJson();
        if (recruitMapper.updatePostRelease(postRelease) > 0) {
            return resultJson.setSuccess("修改成功");
        } else {
            return resultJson.setFail("修改失败");
        }
    }

    @Override
    //添加简历表信息 查询简历表状态
    public ResultJson insertResume(Resume resume) {
        Interview interview = new Interview();
        ResultJson resultJson = new ResultJson();
        if (resume.getResumeState() == 1) {
            if (recruitMapper.insertResume(resume) > 0) {
                Integer resume_id = resume.getId();
                interview.setResumeId(resume_id);
                Event event = new Event();
                event.setName("一面：" + resume.getRealName());
                event.setEventTypeId(1);
                if (recruitMapper.insertEvent(event) > 0) {
                    Integer id = event.getId();
                    interview.setEventId(id);
                    if (recruitMapper.insertInterview(interview) > 0) {
                        return resultJson.setSuccess("添加成功,已推荐面试");
                    }
                }
            }

        }
        if (resume.getResumeState() == null) {
            if (recruitMapper.insertResume(resume) > 0) {
                return resultJson.setSuccess("添加成功,未推荐面试");

            }
        }
        if (resume.getResumeState() == -1) {
            if (recruitMapper.insertResume(resume) > 0) {
                return resultJson.setSuccess("添加成功,取消面试资格");
            }
        }
        return resultJson.setFail("添加失败");
    }

    @Override
    public ResultJson selectResume(Resume resume) {
        List<SelectResumeVo> selectResumeVos = recruitMapper.selectResume(resume);
        ResultJson<Object> objectResultJson = new ResultJson<>();
        objectResultJson.setSuccess("成功", selectResumeVos);
        return objectResultJson;

    }

    @Override
    public ResultJson selectOneResume(Map<String, Integer> map) {
        ResultJson<Object> resultJson = new ResultJson<>();
        SelectResumeVo selectResumeVo = recruitMapper.selectOneResume(map);

        List<Map<String, Object>> events = recruitMapper.selectEvent(map);
        if (selectResumeVo != null) selectResumeVo.setEventList(events);
        resultJson.setSuccess("成功", selectResumeVo);
        return resultJson;
    }

    @Override
    public ResultJson insertInterview(InterviewVo interviewVo) {
        Integer resumeId = interviewVo.getResumeId();

        Map<String, Object> map = new HashMap<>();
        map.put("resumeId", resumeId);
        SelectResumeVo selectResumeVo = recruitMapper.selectOneResume(map);
        Integer state= selectResumeVo.getResumeState();
        ResultJson resultJson = new ResultJson();
        Resume resume = new Resume();
        Event event = new Event();
        Interview interview = new Interview();
        interviewVo.setEventState(1);//更改待办事件状态为已通过
        if (state<4) {
            map.put("eventTypeId", state);
            Integer id  = recruitMapper.selectEventId(map).getId();
            if(id!=null){
            interviewVo.setEventId(id);
            if (recruitMapper.updateEvent(interviewVo) > 0) {
                resume.setResumeState(state+1);
                resume.setId(interviewVo.getResumeId());
                if (recruitMapper.updateResume(resume)>0){//修改简历的状态为推荐二面.三面，录取

                    event.setEventTypeId(state+1);
                    String eventType = recruitMapper.selectEventType(event);
                    event.setName(eventType+":"+ interviewVo.getRealName());
                if (recruitMapper.insertEvent(event) > 0) {//添加代办事件
                    Integer eventId = event.getId();
                    interview.setEventId(eventId);
                    interview.setResumeId(resumeId);
                    if (recruitMapper.insertInterview(interview) > 0) {//添加关联表
                        return resultJson.setSuccess("面试成功");
                    }
                }
            }

        }}}
        if (state==4){
            map.put("eventTypeId", state);
            map.put("resumeId", resumeId);
            Integer id  = recruitMapper.selectEventId(map).getId();
            if (id!=null){
            interviewVo.setEventId(id);
            if (recruitMapper.updateEvent(interviewVo) > 0) {//填写录取信息
                resume.setResumeState(state+1);
                resume.setId(interviewVo.getResumeId());
                if(recruitMapper.updateResume(resume)>0){ //修改简历状态为已录取
                    return resultJson.setSuccess("录取成功");
                }
            }
        }}return resultJson.setFail("出现未知问题,面试失败");
    }

    @Override
    public ResultJson delectInterview(InterviewVo interviewVo) {
        Integer resumeId = interviewVo.getResumeId();

        Map<String, Object> map = new HashMap<>();
        map.put("resumeId", resumeId);
        SelectResumeVo selectResumeVo = recruitMapper.selectOneResume(map);
        Integer state = selectResumeVo.getResumeState();
        ResultJson resultJson = new ResultJson();
        Resume resume = new Resume();
        map.put("eventTypeId", state);
        Integer id = recruitMapper.selectEventId(map).getId();
        if (id != null) {
            interviewVo.setEventState(-1);//更改待办事件状态为不同意
            interviewVo.setEventId(id);
          if ( recruitMapper.updateEvent(interviewVo)>0){
            resume.setResumeState(-1);//修改简历的状态为已删除
            resume.setId(interviewVo.getResumeId());
            if (recruitMapper.updateResume(resume)>0){
                resultJson.setSuccess("成功");
                return resultJson;
            }

        }}resultJson.setFail("失败");
        return resultJson;
    }

    @Override
    public ResultJson updateResume(Resume resume) {
        Interview interview = new Interview();
        ResultJson resultJson = new ResultJson();
        if (resume.getResumeState() == 1) {
            if (recruitMapper.updateResume(resume) > 0) {
                Integer resume_id = resume.getId();
                interview.setResumeId(resume_id);
                Event event = new Event();
                event.setName("一面：" + resume.getRealName());
                event.setEventTypeId(1);
                if (recruitMapper.insertEvent(event) > 0) {
                    Integer id = event.getId();
                    interview.setEventId(id);
                    if (recruitMapper.insertInterview(interview) > 0) {
                        return resultJson.setSuccess("修改成功,已推荐面试");
                    }
                }
            }

        }
        if (resume.getResumeState() == null) {
            if (recruitMapper.updateResume(resume) > 0) {
                Integer resume_id = resume.getId();
                interview.setResumeId(resume_id);
                    return resultJson.setSuccess("修改成功,未推荐面试");

            }
        }
        if (resume.getResumeState() == -1) {
            if (recruitMapper.insertResume(resume) > 0) {
                return resultJson.setSuccess("修改成功,取消面试资格");
            }
        }return resultJson.setFail("修改失败");
}

    @Override
    public ResultJson selectInterviewResume(Resume resume) {
        ResultJson<Object> resultJson = new ResultJson<>();
        List<SelectResumeVo> selectResumeVos = recruitMapper.selectResume(resume);
       List<SelectResumeVo> newSelectResumeVos = new ArrayList<>();
        for (SelectResumeVo selectResumeVo : selectResumeVos) {
            if (selectResumeVo.getResumeState()>0&&selectResumeVo.getResumeState()<5){
                newSelectResumeVos.add(selectResumeVo);
            }
        }return resultJson.setSuccess("成功",newSelectResumeVos);
    }
    /**
     * 储备人才
     *
     * @param map
     * @return
     */
    @Override
    public ResultJson<List<Map<String, Object>>> find(Map<String, Object> map) {

        if (map == null) {
            map = new HashMap<>();
        }
        /*
        * "id:明细id" +
            "currentPage：当前页（默认1）" +
            "pageSize：页面大小（默认6）" +
            "name：职位名称" +
            "depId：所属部门" +
            "state：是否启用（0未启用）*/
        if (map.get("currentPage") == null || map.get("currentPage").equals("")) {
            map.put("currentPage", 1);
        }
        if (map.get("pageSize") == null || map.get("pageSize").equals("")) {
            map.put("pageSize", 6);
        }


        Integer start = (Integer) map.get("pageSize") * ((Integer) map.get("currentPage") - 1);
        map.put("start", start);


        List<Map<String, Object>> mapList = recruitMapper.find(map);



        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        resultJson.setSuccess("查询成功", mapList);


        return resultJson;
    }

}